package com.jy.adscm.dealer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.bean.DealerAttachment;
import com.jy.adscm.dealer.bean.DealerInfo;
import com.jy.adscm.dealer.mapper.DealerAttachmentMapper;
import com.jy.adscm.dealer.mapper.DealerInfoMapper;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.dealer.service.DealerInfoService;
import com.jy.adscm.dealer.service.DealerService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.task.bean.JyJwdTask;
import com.jy.adscm.task.mapper.JyJwdTaskMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-16
 */
@Service
public class DealerServiceImpl extends ServiceImpl<DealerMapper, Dealer> implements DealerService {

   @Autowired
   DealerInfoService  dealerInfoService;
   @Autowired
    DealerAttachmentMapper dealerAttachmentMapper;
   @Autowired
    JyUserMapper jyUserMapper;
   @Autowired
   JyJwdTaskMapper jyJwdTaskMapper;
    @Transactional
    @Override
    public boolean insertDealer(Dealer dealer,Token token) throws AdscmSystemException {
        dealer.setDealerState(0);
        boolean insert = super.insert(dealer);
        //创建三条附件信息
        DealerAttachment dealerAttachment1 =new DealerAttachment();
        dealerAttachment1.setDid(dealer.getId());
        dealerAttachment1.setName("营业执照");
        dealerAttachmentMapper.insert(dealerAttachment1);
        DealerAttachment dealerAttachment2 =new DealerAttachment();
        dealerAttachment2.setDid(dealer.getId());
        dealerAttachment2.setName("开户许可证");
        dealerAttachmentMapper.insert(dealerAttachment2);
        DealerAttachment dealerAttachment3 =new DealerAttachment();
        dealerAttachment3.setDid(dealer.getId());
        dealerAttachment3.setName("医疗器械经营许可证");
        dealerAttachmentMapper.insert(dealerAttachment3);
        dealerInfoService.insertDealerInfo(dealer,token,Dealer.class,dealer.getId());
        JyJwdTask task = new JyJwdTask();
        task.setTableId( Long.valueOf(dealer.getId()));
        task.setTableType(2);//1代表医院信息，2代表经销商信息
        jyJwdTaskMapper.insert(task);
        return insert;
    }
    @Transactional
    @Override
    public boolean deleteDealer(Integer id, Token token) throws AdscmSystemException {
        Dealer dealer = super.selectById(id);
        boolean b = super.deleteById(id);
        dealerInfoService.deleteDealerInfo(dealer,token,Dealer.class,dealer.getId());
        return b;
    }
    @Transactional
    @Override
    public Boolean updateDealer(Dealer dealer, Token token) throws AdscmSystemException {
        Dealer oldDealer = super.selectById(dealer.getId());
        if (dealer.getDealerState()==0 && AdscmBaseUtils.getInstance().isNotEmpty(dealer.getApproveResult(),null)){
            dealer.setApprover(token.getUser().getId().toString());
            dealer.setApproveTime(new Date());
        }

        boolean b = super.updateById(dealer);
        dealerInfoService.updateDealerInfo(dealer,oldDealer,token,Dealer.class,dealer.getId());
        return b;
    }

    @Override
    public List<Dealer> selectAll() throws AdscmSystemException {
        List<Dealer> dealers = super.selectList(null);
        for (Dealer dealer : dealers) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(dealer.getJyManager(),null)){
                JyUser jyUser = jyUserMapper.selectById(dealer.getJyManager());
                dealer.setJyManagerName(jyUser.getName());
            }
        }
        return dealers;
    }

    @Override
    public List<Dealer> selectDealerInfo() {
        Wrapper<Dealer> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("id","name");
        List<Dealer> dealers = super.selectList(wrapper);
        return dealers;
    }
}
